<template>
  <a-modal
    :zIndex="zIndex"
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    width="850px"
    :cancelText="$t('关闭')"
    :confirmLoading="confirmLoading"
    v-drag
  >
    <template slot="footer">
      <a-row :gutter="24">
        <a-col :span="12">
          <div style="text-align: left" v-if="!dataForm.id">
            <a-checkbox v-model="isContinue" @change="changeAddState"></a-checkbox> {{ $t('连续添加') }}
          </div>
        </a-col>
        <a-col :span="12">
          <a-button @click="handleCancel" :disabled="confirmLoading">{{ $t('取消') }}</a-button>
          <a-button type="primary" @click="handleOk" v-if="!disable" :disabled="confirmLoading">{{
            $t('保存')
          }}</a-button>
        </a-col>
      </a-row>
    </template>
    <div style="height: 500px; overflow-y: auto">
      <a-row style="margin-left: 10px">
        <a-col :span="24">
          <a-form-model
            ref="dataForm"
            :rules="rules"
            :model="dataForm"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
            v-enterToNext
          >
            <a-row>
              <div style="margin: 0 0 10px -10px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('基本信息') }}
              </div>
              <a-col :span="12">
                <a-form-model-item prop="code" style="margin-bottom: 10px" :label="$t('商品编码')">
                  <a-input v-model="dataForm.code" ref="code" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item prop="name" style="margin-bottom: 10px" :label="$t('商品名称')">
                  <a-input v-model="dataForm.name" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item prop="kind" style="margin-bottom: 10px" :label="$t('商品分类')">
                  <a-tree-select
                    show-search
                    :filterTreeNode="filterTreeNode"
                    :disabled="disable"
                    allowClear
                    v-model="dataForm.kind"
                    :dropdownStyle="{ maxHeight: '250px', overflow: 'auto' }"
                    :tree-data="treeData"
                    :placeholder="$t('请选择')"
                    :getPopupContainer="triggerNode => { return triggerNode.parentNode || document.body }"
                  />
                </a-form-model-item>
                <a-form-model-item prop="cost" style="margin-bottom: 10px" :label="$t('参考成本')">
                  <a-input-number v-model="dataForm.cost" :disabled="disable"

                                  :formatter="formatter"
                                  :parser="parser"
                                  style="width: 100%;"
                                  :precision="2"
                  ></a-input-number>
                </a-form-model-item>
                <a-form-model-item prop="realCoin" style="margin-bottom: 10px" :label="$t('A票')">
                  <a-input-number v-model="dataForm.realCoin" :disabled="disable"

                                  :formatter="formatter"
                                  :parser="parser"
                                  style="width: 100%;"
                  ></a-input-number>
                </a-form-model-item>
                <a-form-model-item prop="presentCoin" style="margin-bottom: 10px" :label="$t('赠送A票')">
                  <a-input-number v-model="dataForm.presentCoin" :disabled="disable"

                                  :formatter="formatter"
                                  :parser="parser"
                                  style="width: 100%;"
                  ></a-input-number>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-col :span="24" style="padding-right: 0">
                  <a-form-model-item
                    prop="picture"
                    :label="$t('图片')"
                    :labelCol="{ span: 3 }"
                    :wrapperCol="{ span: 18 }"
                  >
                    <div>
                      <j-upload v-model="dataForm.picture" accept="jpg,jpeg,png,gif,webp" fileType="image" :disabled="disable"></j-upload>
                      <div style="color: #f7b926; line-height: 1.2">
                        {{ $t('建议尺寸：750*750，大小不要超过500KB') }}
                      </div>
                    </div>
                  </a-form-model-item>
                </a-col>
              </a-col>
            </a-row>
            <a-row style="margin-left: 20px">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('销售兑换设置') }}
              </div>
              <a-col :span="24">
                <a-row>
                  <a-col :span="6">
                    <a-form-model-item prop="allowMemberDiscount" style="margin-bottom: 5px" :labelCol="{ span: 0 }" :wrapperCol="{ span: 22 }">
                      <a-checkbox :checked="dataForm.allowMemberDiscount == 'Y'" :disabled="disable" @change="(e) => changeSelectState(e, 'allowMemberDiscount', 2)">{{
                        $t('享受会员优惠')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6" v-if="taxSetting == 1">
                    <a-form-model-item prop="allowTax" style="margin-bottom: 5px;margin-left: 10px" :labelCol="{ span: 0 }" :wrapperCol="{ span: 22 }">
                      <a-checkbox :checked="dataForm.allowTax == 1" :disabled="disable" @change="(e) => changeSelectState(e, 'allowTax', 1)">{{
                        $t('销售加税')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-col>
              <a-col :span="24">
                <a-row>
                  <a-col :span="6">
                    <a-form-model-item prop="allowSale" style="margin-bottom: 5px">
                      <a-checkbox :checked="dataForm.allowSale == 'Y'" :disabled="disable" @change="(e) => changeSelectState(e, 'allowSale', 2)">{{ $t('允许销售') }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="price" style="margin-bottom: 5px" :label="$t('售价')">
                      <a-input-number v-model="dataForm.price" :disabled="disable" @change="changePrice"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                                      :precision="2"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6" v-if="taxSetting == 2">
                    <a-form-model-item prop="taxPrice" style="margin-bottom: 5px" :label="$t('其中税费')" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
                      {{dataForm.taxPrice}}
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6" v-if="taxSetting == 2">
                    <a-form-model-item prop="taxPrice" style="margin-bottom: 5px; opacity: 0" :label="$t('其中税费')" :labelCol="{ span: 8 }" :wrapperCol="{ span: 16 }">
                      <a-input-number v-model="dataForm.taxPrice" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                                      :precision="2"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="allowCoin" style="margin-bottom: 5px">
                      <a-checkbox :checked="dataForm.allowCoin == 'Y'" :disabled="disable" @change="(e) => changeSelectState(e, 'allowCoin', 2)">{{ $t('允许币兑换') }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="coin" style="margin-bottom: 5px" :label="$t('代币')">
                      <a-input-number v-model="dataForm.coin" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="allowLottery" style="margin-bottom: 5px" label="" label-width="50px">
                      <a-checkbox :checked="dataForm.allowLottery == 'Y'" :disabled="disable" @change="(e) => changeSelectState(e, 'allowLottery', 2)">{{
                        $t('允许彩票兑换')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="lottery" :label="$t('彩票')" style="margin-bottom: 5px">
                      <a-input-number v-model="dataForm.lottery" ref="lottery" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="allowIntegral" style="margin-bottom: 5px">
                      <a-checkbox :checked="dataForm.allowIntegral == 'Y'" :disabled="disable" @change="(e) => changeSelectState(e, 'allowIntegral', 2)">{{
                        $t('允许积分兑换')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="integral" style="margin-bottom: 5px" :label="$t('积分')">
                      <a-input-number v-model="dataForm.integral" ref="integral" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item
                      prop="allowPoints"
                      :labelCol="{ span: 10 }"
                      :wrapperCol="{ span: 16 }"
                      style="margin-bottom: 5px"
                    >
                      <a-checkbox :checked="dataForm.allowPoints == 'Y'" :disabled="disable" @change="(e) => changeSelectState(e, 'allowPoints', 2)">{{
                        $t('允许A币兑换')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="6">
                    <a-form-model-item prop="points" style="margin-bottom: 5px" :label="$t('A币')">
                      <a-input-number v-model="dataForm.points" ref="points" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-col>
            </a-row>
            <a-row style="margin-left: 20px">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('更多设置') }}
              </div>
              <a-row>
                <a-col :span="2">{{ $t('有效期') }}:</a-col>
                <a-col>
                  <a-form-model-item prop="validityType">
                    <a-radio-group v-model="dataForm.validityType" ref="validityType" @change="limitChangeValid">
                      <div>
                        <a-radio value="0" style="width: 88px" :disabled="disable">{{ $t('不限期') }}</a-radio>
                      </div>
                      <div>
                        <a-radio value="1" style="width: 88px" :disabled="disable">{{ $t('指定日期') }}：</a-radio>
                        <j-date
                          :disabled="!islimitValid"
                          v-model="dataForm.startDate"
                          :placeholder="$t('请选择日期')"
                          style="width: 180px"
                        ></j-date>
                        ~
                        <j-date
                          :disabled="!islimitValid"
                          v-model="dataForm.endDate"
                          :placeholder="$t('请选择日期')"
                          style="width: 180px"
                        ></j-date>
                      </div>
                      <div style="margin-top: 20px">
                        <a-radio value="2" style="width: 88px" :disabled="disable">{{ $t('指定天数') }}：</a-radio>
                        <i18n path="valid-period2" tag="span">
                          <a-input-number
                            place="startDays"
                            v-model="dataForm.startDays"
                            style="width: 64px; margin: 0 4px"
                            :disabled="islimitValid || disable"
                            :min="0"
                          />
                          <a-input-number
                            place="validDays"
                            v-model="dataForm.validDays"
                            style="width: 64px; margin: 0 4px"
                            :disabled="islimitValid || disable"
                            :min="0"
                          />
                        </i18n>
                      </div>
                    </a-radio-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="9">
                  <a-form-model-item prop="state" style="margin-bottom: 5px" :label="$t('上架状态')">
                    <a-radio-group v-model="dataForm.state" ref="state" :disabled="disable">
                      <a-radio value="Y">{{ $t('上架') }}</a-radio>
                      <a-radio value="N">{{ $t('下架') }}</a-radio>
                    </a-radio-group>
                  </a-form-model-item>
                </a-col>
                <a-col :span="9">
                  <a-form-model-item style="margin-bottom: 5px">
                    <a-checkbox :checked="dataForm.authSale == 1" :disabled="disable" @change="(e) => changeSelectState(e, 'authSale', 1)">{{
                      $t('授权销售')
                    }}</a-checkbox>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row v-if="stateDisable != 8 && stateDisable != 10 && stateDisable != 11">
                <a-col :span="9">
                  <a-form-model-item prop="limitCycle" style="margin-bottom: 5px" :label="$t('限购类型')">
                    <j-dict-select-tag
                      :placeholder="$t('请选择')"
                      style="width: 100px"
                      v-model="dataForm.limitCycle"
                      @change="changelimitCycleState"
                      :disabled="disable"
                      dictCode="bis_goods_infor.limit_cycle"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="7" style="margin-left: -110px">
                  <a-form-model-item
                    prop="limitNum"
                    :labelCol="{ span: 7 }"
                    :wrapperCol="{ span: 16 }"
                    style="margin-bottom: 0px"
                    :label="$t('限购数量')"
                  >
                    <a-input v-model="dataForm.limitNum" :disabled="limitCycledisable"></a-input>
                  </a-form-model-item>
                </a-col>
                <a-col :span="10">
                  <div style="color: #f6aa64; margin-top: 5px">
                    {{ $t('(每个会员在特定的限购周期内最大允许的购买数量)') }}
                  </div>
                </a-col>
              </a-row>
              <a-row>
                <a-col>
                  <a-form-model-item
                    prop="allowMemberType"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-bottom: 5px; margin-left: 4px"
                    :label="$t('适用会员')"
                  >
                    <a-radio-group
                      v-model="allowMemberType"
                      @change="getTypeValue"
                      :disabled="disable"
                      style="margin-left: 5px"
                    >
                      <a-radio value="">{{ $t('全部') }}</a-radio>
                      <a-radio value="part">{{ $t('指定会员') }}</a-radio> </a-radio-group
                    ><br />
                    <a-checkbox-group
                      :span="24"
                      v-if="memberShow"
                      v-model="dataForm.allowMemberTypes"
                      style="margin-top: 6px; margin-left: 5px"
                    >
                      <a-checkbox style="margin-left: 8px" :value="item.id" v-for="item in memberList" :key="item.id">{{
                        item.name
                      }}</a-checkbox>
                    </a-checkbox-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col>
                  <a-form-model-item
                    prop="allowDeviceType"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-bottom: 5px; margin-left: 4px"
                    :label="$t('适用渠道')"
                  >
                    <a-radio-group
                      v-model="allowDeviceType"
                      @change="getDeviceTypeValue"
                      :disabled="disable"
                      style="margin-left: 5px"
                    >
                      <a-radio value="">{{ $t('全部') }}</a-radio>
                      <a-radio value="part">{{ $t('指定渠道') }}</a-radio>
                    </a-radio-group>
                    <br />
                    <a-checkbox-group
                      v-if="deviceTypeShow"
                      v-model="dataForm.allowDeviceTypes"
                      style="margin-top: 6px; margin-left: 5px"
                      :disabled="disable"
                      @change="getDeviceList"
                    >
                      <a-checkbox
                        style="margin-left: 8px"
                        :value="item.type"
                        v-for="item in deviceTypeList"
                        :key="item.type"
                        >{{ item.type_dictText }}
                      </a-checkbox>
                    </a-checkbox-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col>
                  <a-form-model-item
                    prop="allowDevice"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-bottom: 5px; margin-left: 4px"
                    :label="$t('销售终端')"
                  >
                    <a-radio-group
                      v-model="allowDevice"
                      @change="getDeviceValue"
                      :disabled="disable"
                      style="margin-left: 5px"
                    >
                      <a-radio value="">{{ $t('全部') }}</a-radio>
                      <a-radio value="part">{{ $t('指定终端') }}</a-radio>
                    </a-radio-group>
                    <br />
                    <a-checkbox-group
                      v-if="deviceShow"
                      v-model="dataForm.allowDevices"
                      style="margin-top: 6px; margin-left: 5px"
                      :disabled="disable"
                    >
                      <a-checkbox style="margin-left: 8px" :value="item.id" v-for="item in deviceList" :key="item.id"
                        >{{ item.name }}
                      </a-checkbox>
                    </a-checkbox-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    prop="comment"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    :label="$t('商品简介')"
                    style="margin-left: 4px"
                  >
                    <a-textarea v-model="dataForm.comment" ref="comment" :rows="2" :disabled="disable" />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    prop="details"
                    :label="$t('商品详情')"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-left: 4px"
                  >
                    <div style="color: #e28909">
                      {{
                        $t('用于小程序商品详情的展示；建议图片宽度375px，上传多张图片时，每张图片的宽度应该保持一致。')
                      }}
                    </div>
                    <j-upload
                      v-model="dataForm.details"
                      accept="jpg,jpeg,png,gif,webp"
                      fileType="image"
                      :disabled="disable"
                      class="detail-upload"
                      text="upload"
                    ></j-upload>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-row>
            <!-- <a-row style="margin-left: 20px" v-if="stateDisable != 11">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">{{$t('第三方核销')}}</div>
              <a-form-model-item>
                <div style="display: inline-block" v-for="(item, index, key) in dataForm.bisGoodsThirdList" :key="key">
                  {{ item.channel_dictText }}: {{ item.thirdCode }}
                  <a-icon
                    @click="delGoodsThird(item, index, 'exclude')"
                    style="color: #1890ff; cursor: pointer"
                    type="close"
                    :disabled="disable"
                    v-if="!disable"
                  />，
                </div>
                <span>
                  <a href="javascript:void(0);" @click="addGoodsThird()" style="margin-left: 10px" v-if="!disable"
                    >{{$t('添加')}}</a
                  >
                </span>
              </a-form-model-item>
            </a-row> -->
          </a-form-model>
        </a-col>
      </a-row>
    </div>
    <bis-goods-third-modal ref="goodsThirdmodel" @ok="addGoodsThirdCallback"></bis-goods-third-modal>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import BisGoodsThirdModal from './BisGoodsThirdModal'
import {formatter, parser} from "@api/comm";
let _self
export default {
  name: 'GoodsInfoALotteryModal',
  components: {
    BisGoodsThirdModal,
  },
  data() {
    return {
      formatter,
      parser,
      zIndex: 1001,
      form: this.$form.createForm(this),
      title: '',
      confirmLoading: false,
      dataForm: {
        code: '',
        name: '',
        allowOut: 'Y',
        allowWarn: 'N',
        warnNum: 0,
        verifyType: '1',
        allowMemberDiscount: 'Y',
        allowTax: 1,
        allowCoin: 'N',
        allowLottery: 'N',
        allowPoints: 'N',
        allowSale: 'Y',
        allowIntegral: 'N',
        allowAlottery: 'N',
        state: '',
        authSale: 0,
        allowMemberType: '',
        allowDeviceType: '',
        allowDevice: '',
        form: '', //形态
        bisGoodsPackageList: [],
        validityType: '0',
        bisGoodsThirdList: [],
      },
      treeData: [],
      deviceTypeShow: false,
      deviceShow: false,
      memberShow: false,
      visible: false,
      labelCol: {
        xs: { span: 20 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 15 },
      },
      rules: {
        supplierId: [{ required: true, message: this.$t('请选择所属商户') }],
        name: [{ required: true, message: this.$t('请输入卖品名称') }],
        kind: [{ required: true, message: this.$t('请选择卖品分类') }],
        cost: [
          { required: true, message: this.$t('请输入卖品参考成本') },
          { pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') },
        ],
        realCoin: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        presentCoin: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        price: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        integral: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        lottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        alottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        points: [{ pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') }],
        referencePrice: [{ required: true, message: this.$t('请输入店面售价') }],
        cost1: [{ required: true, message: this.$t('请输入结算价') }],
      },
      memberList: [],
      deviceTypeList: [],
      deviceList: [],
      url: {
        add: '/goods/bisGoodsInfor/add',
        edit: '/goods/bisGoodsInfor/edit',
        importExcelUrl: '/sys/common/upload',
      },
      disable: false,
      limitCycledisable: false,
      allowWarnsdisable: true,
      list: [],
      allowMemberType: '',
      allowDevice: '',
      allowDeviceType: '',
      stateDisable: '',
      taxSetting: 0,
      islimitValid: false,
      allChildKind: [],

      goodsPriceRule: {
        isGoodsPriceRule: 0,
        coinRule: 1,
        lotteryRule: 1,
        integralRule: 1,
        pointsRule: 1,
        alotteryRule: 1,
      },
      isContinue:true
    }
  },
  mounted() {},
  created() {
    _self = this
  },
  methods: {
    addGoodsThird() {
      this.$refs.goodsThirdmodel.edit()
    },
    delGoodsThird(item) {
      this.dataForm.bisGoodsThirdList.forEach((col) => {
        if (col.channel == item.channel && col.thirdCode == item.thirdCode) {
          let index = this.dataForm.bisGoodsThirdList.indexOf(col)
          this.dataForm.bisGoodsThirdList.splice(index, 1)
        }
      })
    },
    addGoodsThirdCallback(item) {
      getAction('/sys/dict/getDictText/bis_goods_third.channel/' + item.channel).then((res) => {
        if (res.success) {
          item.channel_dictText = res.result
          var b = true
          if (this.dataForm.bisGoodsThirdList && this.dataForm.bisGoodsThirdList.length > 0) {
            this.dataForm.bisGoodsThirdList.forEach((col) => {
              if (col.channel == item.channel && col.thirdCode == item.thirdCode) {
                b = false
                return
              }
            })
          } else {
            this.$set(this.dataForm, 'bisGoodsThirdList', [])
          }
          if (b) {
            this.dataForm.bisGoodsThirdList.push(item)
          } else {
            this.$message.warning('该第三方渠道商品已存在')
            return
          }
        }
      })
    },
    changePrice() {
      let that = this
      if (that.goodsPriceRule.isGoodsPriceRule == 1) {
        that.$set(that.dataForm, 'coin', that.dataForm.price * that.goodsPriceRule.coinRule)
        that.$set(that.dataForm, 'lottery', that.dataForm.price * that.goodsPriceRule.lotteryRule)
        that.$set(that.dataForm, 'integral', that.dataForm.price * that.goodsPriceRule.integralRule)
        that.$set(that.dataForm, 'points', that.dataForm.price * that.goodsPriceRule.pointsRule)
        that.$set(that.dataForm, 'alottery', that.dataForm.price * that.goodsPriceRule.alotteryRule)
      }
      if(that.taxSetting == 2){
        that.$set(that.dataForm, 'taxPrice', (that.dataForm.price - (that.dataForm.price / ( 1 + localStorage.getItem('taxRate') / 100))).toFixed(2))
      }
    },
    getInfo() {
      let that = this
      getAction('/bis/bisSysParameter/list', { name: 'isGoodsPriceRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.goodsPriceRule.isGoodsPriceRule = res.result.records[0].ivalue
            if (that.goodsPriceRule.isGoodsPriceRule == 1) {
              getAction('/bis/bisSysParameter/list', {
                name: 'coinRule,lotteryRule,integralRule,pointsRule,alotteryRule',
              }).then((res) => {
                if (res.success) {
                  if (res.result.total > 0) {
                    res.result.records.forEach((el) => {
                      if (el.name == 'coinRule') {
                        that.goodsPriceRule.coinRule = el.dvalue
                      }
                      if (el.name == 'lotteryRule') {
                        that.goodsPriceRule.lotteryRule = el.dvalue
                      }
                      if (el.name == 'integralRule') {
                        that.goodsPriceRule.integralRule = el.dvalue
                      }
                      if (el.name == 'pointsRule') {
                        that.goodsPriceRule.pointsRule = el.dvalue
                      }
                      if (el.name == 'alotteryRule') {
                        that.goodsPriceRule.alotteryRule = el.dvalue
                      }
                    })
                  }
                } else {
                  that.$message.error(res.message)
                }
              })
            }
          }
        } else {
          that.$message.error(res.message)
        }
      })
    },
    add(value) {
      this.visible = true
      this.stateDisable = value
      this.isContinue = localStorage.getItem('goodsAddInfo')
        ? JSON.parse(localStorage.getItem('goodsAddInfo')).state
        : true
      this.islimitValid = true
      this.dataForm = {
        code: '',
        name: '',
        allowOut: 'Y',
        allowWarn: 'N',
        warnNum: 0,
        verifyType: '1',
        cost: 0,
        allowMemberDiscount: 'Y',
        allowTax: 1,
        allowCoin: 'N',
        allowLottery: 'N',
        allowPoints: 'N',
        allowSale: 'Y',
        allowIntegral: 'N',
        allowAlottery: 'N',
        state: 'Y',
        authSale: 0,
        limitCycle: '0',
        allowMemberType: '',
        allowDevices: [],
        allowDevice: '',
        allowDeviceTypes: [],
        allowDeviceType: '',
        form: value,
        presentCoin: '0',
        realCoin: '0',
        species: '1',
        bisGoodsPackageList: [],
        validityType: '0',
      }
      this.edit(this.dataForm)
    },
    edit(record, state) {
      this.getInfo()
      this.stateDisable = record.form
      this.disable = state
      this.getMemberList()
      this.getDeviceTypeList()
      this.taxSetting = localStorage.getItem('taxSetting')
      this.select()
      initDictOptions('bis_goods_infor.verify_type').then((res) => {
        if (res.success) {
          this.list = res.result
        }
      })
      this.visible = true
      if (record.validityType == '1') {
        this.islimitValid = true
      } else {
        this.islimitValid = false
      }

      if (this.disable == true || record.limitCycle == '0') {
        this.limitCycledisable = true
      }
      if (record.allowWarn == 'Y' || this.disable == true) {
        this.allowWarnsdisable = true
      }
      if (record.id != undefined) {
        getAction('/goods/bisGoodsThird/list', { goodsId: record.id, pageSize: 999 }).then((res) => {
          if (res.success && res.result.total > 0) {
            this.$set(this.dataForm, 'bisGoodsThirdList', res.result.records)
          }
        })
      }

      this.allowMemberType = record.allowMemberType
      if (this.allowMemberType == '' || this.allowMemberType == null || this.allowMemberType == undefined) {
        this.memberShow = false
      } else {
        record.allowMemberTypes = record.allowMemberType.split(',')
        this.allowMemberType = 'part'
        this.memberShow = true
      }

      this.allowDeviceType = record.allowDeviceType
      if (this.allowDeviceType == null || this.allowDeviceType == undefined) {
        this.allowDeviceType = ''
      }
      if (this.allowDeviceType == '') {
        this.deviceTypeShow = false
      } else {
        record.allowDeviceTypes = record.allowDeviceType.split(',')
        this.allowDeviceType = 'part'
        this.deviceTypeShow = true
      }

      this.allowDevice = record.allowDevice
      if (this.allowDevice == '' || this.allowDevice == null || this.allowDevice == undefined) {
        this.deviceShow = false
      } else {
        record.allowDevices = record.allowDevice.split(',')
        this.allowDevice = 'part'
        this.deviceShow = true
      }
      this.dataForm = Object.assign({}, record)
      this.$set(this.dataForm, 'cost1', this.dataForm.cost)
      this.getDeviceList()
    },
    filterTreeNode(inputValue, treeNode) {
      // 假设输入格式为 "级别:关键词" 如 "2:电器"
      const match = inputValue.match(/([123]):(.+)/)
      if (match) {
        const level = parseInt(match[1])
        const keyword = match[2]
        const nodeLevel = this.getNodeLevel(treeNode)
        
        if (nodeLevel !== level) return false
        return treeNode.data.props.title.includes(keyword)
      }
      
      // 默认搜索
      return treeNode.data.props.title.includes(inputValue)
    },
    getNodeLevel(node) {
      let level = 1
      let parent = node.parent
      while (parent) {
        level++
        parent = parent.parent
      }
      return level
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
        if (res.success) {
          that.allChildKind = []
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, this.$t('提示'), {
            confirmButtonText: this.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      getAction('/goods/bisGoodsKind/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      let that = this
      return result.map((item) => {
        if (item.hasChild == 0 && item.key != '0') {
          that.allChildKind.push(item.key)
        }
        return item
      })
    },
    limitChangeValid(e) {
      if (e.target.value == '1') {
        this.islimitValid = true
      } else {
        this.islimitValid = false
      }
    },
    changelimitCycleState(e) {
      if (e == '0') {
        this.limitCycledisable = true
      } else {
        this.limitCycledisable = false
      }
    },
    getTypeValue(e) {
      if (e.target.value == 'part') {
        this.memberShow = true
      } else {
        this.memberShow = false
      }
    },
    getDeviceTypeValue(e) {
      if (e.target.value == 'part') {
        this.deviceTypeShow = true
      } else {
        this.deviceTypeShow = false
      }
    },
    getDeviceValue(e) {
      if (e.target.value == 'part') {
        this.deviceShow = true
      } else {
        this.deviceShow = false
      }
    },
    getMemberList() {
      getAction('/member/bisMemberRule/list').then((res) => {
        if (res.success) {
          this.memberList = res.result.records
        }
      })
    },
    getDeviceTypeList() {
      getAction('/device/bisDeviceInfo/getTypeList', { kind: 0 }).then((res) => {
        if (res.success) {
          this.deviceTypeList = res.result.records
        }
      })
    },
    getDeviceList() {
      let that = this
      let kind = 0
      let type = ''
      if (that.allowDeviceType == 'part') {
        if (that.dataForm.allowDeviceTypes) {
          type = that.dataForm.allowDeviceTypes.join(',')
        }
      }
      getAction('/device/bisDeviceInfo/nameList', {
        pageSize: 999,
        type: type,
        kind: kind,
        column: 'type',
        order: 'ASC',
      }).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      let querydata = {}
      that.$refs.dataForm.validate((valid) => {
        if (valid) {
          if (that.dataForm.allowLottery == 'Y' && (that.dataForm.lottery == undefined || that.dataForm.lottery === '')) {
            that.$message.warn(that.$t('彩票值不能为空'))
            return
          }
          if (that.dataForm.allowCoin == 'Y' && (that.dataForm.coin == undefined || that.dataForm.coin === '')) {
            that.$message.warn(that.$t('代币值不能为空'))
            return
          }
          if (that.dataForm.allowPoints == 'Y' && (that.dataForm.points == undefined || that.dataForm.points === '')) {
            that.$message.warn(that.$t('A币不能为空'))
            return
          }
          if (that.dataForm.allowSale == 'Y' && (that.dataForm.price == undefined || that.dataForm.price === '')) {
            that.$message.warn(that.$t('售价不能为空'))
            return
          }
          if (that.dataForm.allowIntegral == 'Y' && (that.dataForm.integral == undefined || that.dataForm.integral === '')) {
            that.$message.warn(that.$t('积分不能为空'))
            return
          }
          if (that.dataForm.allowAlottery == 'Y' && (that.dataForm.alottery == undefined || that.dataForm.alottery === '')) {
            that.$message.warn(that.$t('A票值不能为空'))
            return
          }
          if (that.dataForm.limitCycle != '0') {
            if (that.dataForm.limitNum == undefined || that.dataForm.limitNum === '') {
              that.$message.warn(that.$t('限购数量不能为空'))
              return
            }
          }
          if (that.allowMemberType == 'part') {
            if (that.dataForm.allowMemberTypes == null || that.dataForm.allowMemberTypes == '') {
              that.$message.warn(that.$t('请选择会员类型'))
              return
            }
            that.dataForm.allowMemberType = that.dataForm.allowMemberTypes.join(',')
          } else {
            that.dataForm.allowMemberType = ''
          }
          if (that.allowDeviceType == 'part') {
            if (that.dataForm.allowDeviceTypes == null || that.dataForm.allowDeviceTypes == '') {
              that.$message.warn(that.$t('请选择适用终端'))
              return
            }
            that.dataForm.allowDeviceType = that.dataForm.allowDeviceTypes.join(',')
          } else {
            that.dataForm.allowDeviceType = ''
          }

          if (that.allowDevice == 'part') {
            if (that.dataForm.allowDevices == null || that.dataForm.allowDevices == '') {
              that.$message.warn(that.$t('请选择适用终端'))
              return
            }
            that.dataForm.allowDevice = that.dataForm.allowDevices.join(',')
          } else {
            that.dataForm.allowDevice = ''
          }
          querydata = that.dataForm
          let httpurl = ''
          let method = ''
          if (!that.dataForm.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          that.confirmLoading = true
          httpAction(httpurl, querydata, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                if (that.dataForm.id || !that.isContinue) {
                  that.close()
                }else{
                  that.initDataForm()
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    changeAddState() {
      localStorage.setItem('goodsAddInfo', JSON.stringify({ state: this.isContinue }))
    },
    initDataForm(){
      let value=this.stateDisable
      this.islimitValid = true
      this.dataForm = {
        code: '',
        name: '',
        allowOut: 'Y',
        allowWarn: 'N',
        warnNum: 0,
        verifyType: '1',
        cost: 0,
        allowMemberDiscount: 'Y',
        allowCoin: 'N',
        allowLottery: 'N',
        allowPoints: 'N',
        allowSale: 'Y',
        allowIntegral: 'N',
        allowAlottery: 'N',
        state: 'Y',
        authSale: 0,
        limitCycle: '0',
        allowMemberType: '',
        allowDevices: [],
        allowDevice: '',
        allowDeviceTypes: [],
        allowDeviceType: '',
        form: value,
        presentCoin: '0',
        realCoin: '0',
        species: '1',
        bisGoodsPackageList: [],
        validityType: '0',
      }
      this.$refs.code.focus()
      this.edit(this.dataForm)
    },
    changeSelectState(e, key, type){
      if(type == 1){
        this.$set(this.dataForm, key, e.target.checked ? 1 : 0)
      }
      if(type == 2){
        this.$set(this.dataForm, key, e.target.checked ? 'Y' : 'N')
      }
    }
  },
}
</script>
<style scoped>
/deep/ .detail-upload .ant-upload-list-picture-card-container {
  width: 100%;
  height: 250px;
}
/deep/ .detail-upload .ant-upload-list-picture-card .ant-upload-list-item {
  width: 77% !important;
  height: 100% !important;
}
/deep/ .detail-upload .ant-upload .ant-upload-select .ant-upload-select-picture-card {
  width: 100%;
  height: 250px;
}
/deep/ .detail-upload .ant-upload {
  width: 77% !important;
  height: 100% !important;
}
</style>